package com.zygh.lz.controller.pc;

import com.alibaba.fastjson.JSONObject;
import com.zygh.lz.entity.EquipageType;
import com.zygh.lz.entity.dto.PostDeployDTO;
import com.zygh.lz.entity.query.LongFengSignQuery;
import com.zygh.lz.entity.query.PostDeployCoordinateQuery;
import com.zygh.lz.entity.query.PostDeployQuery;
import com.zygh.lz.entity.vo.EquipageDetailVO;
import com.zygh.lz.entity.vo.EquipageVO;
import com.zygh.lz.entity.vo.PostDeployVO;
import com.zygh.lz.entity.vo.XareaVO;
import com.zygh.lz.service.EquipageTypeService;
import com.zygh.lz.service.PostDeployService;
import com.zygh.lz.service.SignEquipageService;
import com.zygh.lz.service.impl.LongFengService;
import com.zygh.lz.service.impl.SignEquipageServiceImpl;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.util.DateUtils;
import com.zygh.lz.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: zygh-transform
 * @description: 岗位部署相关API
 * @author: jhaol
 * @created: 2023/04/07 15:01
 */

@Slf4j
@Api(tags = "岗位部署相关API")
@RestController
public class PostDeployController {
    @Resource
    private EquipageTypeService equipageTypeService;

    @Resource
    private PostDeployService postDeployService;

    @Resource
    private LongFengService longFengService;

    @Resource
    private SignEquipageService signEquipageService;

    @ApiOperation("获取所有装备的类型")
    @GetMapping("/equipage/type")
    public ResultBean getEquipageType() {
        return ResultUtil.setOK("success", equipageTypeService.lambdaQuery().eq(EquipageType::getHide, 0).groupBy(EquipageType::getTypeName).list());
    }


    @ApiOperation("根据条件搜索岗位部署信息")
    @GetMapping("/postDeploy")
    public ResultBean<PostDeployVO> postDeploy(PostDeployQuery postDeployQuery) {

        return ResultUtil.setOK("success", postDeployService.getPostDeploy(postDeployQuery));
    }

    @ApiOperation("根据条件搜索特殊勤务岗位部署信息")
    @GetMapping("/getSpecialPostDeploy")
    public ResultBean<PostDeployVO> getSpecialPostDeploy(PostDeployQuery postDeployQuery) {

        return ResultUtil.setOK("success", postDeployService.getSpecialPostDeploy(postDeployQuery));
    }

    @ApiOperation("根据岗位ID获取特殊岗位部署信息")
    @GetMapping("/specialPostDeploy/{id}")
    public ResultBean<XareaVO> getSpecialPostDeployById(@PathVariable Long id) {
        return ResultUtil.setOK("success", postDeployService.getSpecialPostDeployById(id));
    }

    @ApiOperation("根据岗位ID获取岗位部署信息")
    @GetMapping("/postDeploy/{id}")
    public ResultBean<XareaVO> getPostDeployById(@PathVariable Long id) {
        return ResultUtil.setOK("success", postDeployService.getPostDeployById(id));
    }

    /**
     * 根据id获取装备详细信息
     **/

    @ApiOperation("根据gpsid获取装备详细信息")
    @GetMapping("/equipage/detail/{gpsid}")
    public ResultBean<EquipageDetailVO> getEquipageDetailById(@PathVariable String gpsid) {
        return ResultUtil.setOK("success", postDeployService.getEquipageDetailById(gpsid));
    }

    @ApiOperation("根据警号和装备类型获取警员绑定的装备详细信息")
    @GetMapping("/equipage/detail/policeNumber/{policeNumber}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "policeNumber", value = "警号", dataType = "String", required = true),
            @ApiImplicitParam(name = "typeCode", value = "装备类型编号", dataType = "Integer", required = true)
    })
    public ResultBean<List<EquipageDetailVO>> getEquipageDetailByPoliceNumber(@PathVariable String policeNumber, @RequestParam String typeCode) {
        List<String> gpsIds = signEquipageService.getEquipageGpsIdListByPoliceNumberAndTypeCode(policeNumber, typeCode);
        List<EquipageDetailVO> equipageDetailVOList = new ArrayList<>();
        if (gpsIds.size() > 0) {
            equipageDetailVOList = postDeployService.getEquipageDetailByIds(gpsIds);
        }
        return ResultUtil.setOK("success", equipageDetailVOList);
    }


    @ApiOperation("获取坐标数据")
    @PostMapping("/postDeploy/getCoordinate")
    public ResultBean<List<PostDeployDTO>> getCoordinate(@RequestBody PostDeployCoordinateQuery postDeployCoordinateQuery) {
        return ResultUtil.setOK("success", postDeployService.getCoordinate(postDeployCoordinateQuery));
    }

    @ApiOperation("获取特殊勤务坐标数据")
    @PostMapping("/postDeploy/getSpecialPostDeployCoordinate")
    public ResultBean<List<PostDeployDTO>> getSpecialPostDeployCoordinate(@RequestBody PostDeployCoordinateQuery postDeployCoordinateQuery) {
        return ResultUtil.setOK("success", postDeployService.getSpecialPostDeployCoordinate(postDeployCoordinateQuery));
    }

    @ApiOperation("市局勤务获取任务组")
    @GetMapping("/postDeploy/getTaskGroupTree")
    public ResultBean getTaskGroupTree() {
        return longFengService.getTaskGroupTreePc();
    }

    @ApiOperation("市局勤务获取组织机构")
    @GetMapping("/postDeploy/tree")
    public ResultBean getTree() {
        return ResultUtil.setOK("success", longFengService.getTree());
    }

    @ApiOperation("获取市局签到记录")
    @PostMapping("/postDeploy/getSign")
    public ResultBean getSign(@RequestBody LongFengSignQuery longFengSignQuery) {
        if (StringUtils.isEmpty(longFengSignQuery.getBeginDateTime())) {
            longFengSignQuery.setBeginDateTime(DateUtils.getDayBegin());
            longFengSignQuery.setEndDateTime(DateUtils.getDayEnd());
        }
        if (longFengSignQuery.getOnlineStatus() == null || longFengSignQuery.getOnlineStatus() == -1) {
            longFengSignQuery.setOnlineStatus(null);
        }
        JSONObject sign = longFengService.getSign(longFengSignQuery.getCurrent(), longFengSignQuery.getSize(),
                longFengSignQuery.getBeginDateTime(),
                longFengSignQuery.getEndDateTime(), longFengSignQuery.getPoliceName(), longFengSignQuery.getPoliceCode(),
                longFengSignQuery.getOnlineStatus(), longFengSignQuery.getGroupIds(), longFengSignQuery.getDeptIds());

        sign.put("list", sign.getJSONArray("records"));
        sign.remove("records");
        return ResultUtil.setOK("success", sign);
    }
}
